package alchemist.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import alchemist.model.Element;
import alchemist.service.CombinationService;
import alchemist.service.LoggedUserProvider;

import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.types.User;

@Controller
@RequestMapping( "/combination" )
public class CombinationsController
{
    @Autowired
    private CombinationService combinationService;
    @Autowired
    private LoggedUserProvider loggedUserProvider;
    

    @RequestMapping( value = "/seba", method = RequestMethod.GET )
    public User test( String code )
    {
		FacebookClient facebookClient = new DefaultFacebookClient( code );
		User user = facebookClient.fetchObject("me", User.class);
    	return user;
    }
    
    @RequestMapping( value = "/login", method = RequestMethod.GET )
    public void test( HttpServletResponse response ) throws IOException
    {
        String url = "https://www.facebook.com/dialog/oauth?client_id=129272903843609" +
                "&redirect_uri=http://localhost:8888/api/combination/seba?";
        response.sendRedirect( url );
    }

    @RequestMapping( value = "/combine", method = RequestMethod.GET )
    public Set< Element > combine( String element1, String element2, HttpServletRequest request ) throws IOException, NumberFormatException
    {
        String userId = loggedUserProvider.getCurrentUserId( request );
        return this.combinationService.combine( element1, element2, userId );
    }

    @RequestMapping( value = "/basic", method = RequestMethod.GET )
    public Set< Element > getBasic()
    {
        return this.combinationService.getBasicElements();
    }

    @RequestMapping( value = "/discovered", method = RequestMethod.GET )
    public Map< String, Object > getDiscovered( HttpServletRequest request ) 
    {
        String userId = this.loggedUserProvider.getCurrentUserId( request );
        final Map< String, Object > result = new HashMap< String, Object >();
        result.put( "user", userId );
        result.put( "total", this.combinationService.getTotalCombinations().toString() );
        result.put( "elementList", this.combinationService.getDiscovered( userId ) );
        return result;
    }

    @ExceptionHandler
    public Map< String, String > handleException( Exception ex, HttpServletRequest request )
    {
        final Map< String, String > errors = new HashMap< String, String >();
        errors.put( "error", "An error has occurred" );
        return errors;
    }
    
}
